from flask import current_app
from flask import g, jsonify
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for

from info import db
from info.models import User
from info.modules import index
from info.response_code import RET
from utils.common import user_login_data
from . import profile_blu


# 用户中心
@profile_blu.route('/info')
@user_login_data
def user_info():
    user = g.user
    if not user:
        return redirect('/')
    data ={
        'user':user
    }
    return render_template('news/user.html',data = data)


# 基本信息
@profile_blu.route('/base_info',methods=['GET','POST'])
@user_login_data
def base_info():
    user = g.user
    # GET请求处理
    if request.method == 'GET':
        data = {
            'user': user.to_dict()
        }
        return render_template('news/user_base_info.html', data=data)

    # POST请求
    #   获取参数
    nick_name = request.json.get('nick_name')
    signature = request.json.get('signature')
    gender = request.json.get('gender')
    #   校验参数
    if not all([nick_name,signature,gender]):
        return jsonify(errno = RET.PARAMERR,errmsg = "参数错误")
    if gender not in ['MAN','WOMEN']:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    # 将数据添加到User数据表中
    user.nick_name = nick_name
    user.signature = signature
    user.gender = gender
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno = RET.DBERR,errmsg = "数据提交失败")
    # 返回数据
    return jsonify(errno = RET.OK,errmsg = "成功")


# 头像
@profile_blu.route('/pic_info')
@user_login_data
def pic_info():
    user = g.user

    data = {
        'user': user
    }
    return render_template('news/user_pic_info.html', data=data)


# 密码
# URL:/pass_info
# 请求方式：GET、POST
# 参数：old_password,new_password
@profile_blu.route('/pass_info',methods=['GET','POST'])
@user_login_data
def pass_info():
    user = g.user
    # GET请求
    if request.method == 'GET':
        data = {
            'user': user.to_dict()
        }
        return render_template('news/user_pass_info.html', data=data)

    # POST请求
    #   获取参数
    old_password = request.json.get('old_password')
    new_password = request.json.get('new_password')
    #   校验参数
    if not all([old_password,new_password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    if not user.check_passowrd(old_password):
        return jsonify(errno = RET.PARAMERR,errmsg="密码输入错误")
    user.password = new_password
    return jsonify(errno = RET.OK,errmsg = "成功")


    # 用户收藏
@profile_blu.route('/collection')
@user_login_data
def user_collection():
    user = g.user
    try:

    data = {
        'user': user
    }
    return render_template('news/user_collection.html', data=data)


# 我的关注
@profile_blu.route('/follow')
@user_login_data
def user_follow():
    user = g.user
    data = {
        'user': user
    }
    return render_template('news/user_follow.html', data=data)


# 新闻发布
@profile_blu.route('/news_release')
@user_login_data
def news_release():
    user = g.user
    data = {
        'user': user
    }
    return render_template('news/user_news_release.html', data=data)


# 新闻列表
@profile_blu.route('/news_list')
@user_login_data
def news_list():
    user = g.user
    data = {
        'user': user
    }
    return render_template('news/user_news_list.html', data=data)
